package com.picturestory.client.android.registration;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import com.picturestory.client.android.R;
import com.picturestory.client.android.util.Constants;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.telephony.TelephonyManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class ServiceInfo extends Activity {

	private String idNumber;
	
	private Spinner mCountryCodeSpinner;
	private Button mNext;
	private EditText mCountryCode;
	private EditText mPhoneNumber;
	private TextView mTVCountryCodeLabel;
	private TextView mTVPhoneNumberLabel;
	
	private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			String mockPin = "1";
			FileOutputStream fos;
			try {
				fos = openFileOutput(Constants.USER_FILE, Context.MODE_PRIVATE);
				fos.write(mockPin.getBytes());
				fos.close();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			Intent intent = new Intent(ServiceInfo.this, Consent.class);
			intent.putExtra("country", mCountryCode.getText().toString());
			intent.putExtra("idNumber", mPhoneNumber.getText().toString());
			startActivity(intent);
		}
		
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.service_info);
		
		TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
		idNumber = tm.getLine1Number();
		
		mCountryCodeSpinner = (Spinner) findViewById(R.id.list_of_country_codes);
		mNext = (Button) findViewById(R.id.service_info_next);
		mCountryCode = (EditText) findViewById(R.id.country_code);
		mPhoneNumber = (EditText) findViewById(R.id.phone_number);
		
		mTVCountryCodeLabel = (TextView) findViewById(R.id.country_code_label);
		mTVPhoneNumberLabel = (TextView) findViewById(R.id.phone_number_label);
		
		Typeface font = Typeface.createFromAsset(getAssets(), "perpetua.ttf");
		
		mTVCountryCodeLabel.setTypeface(font);
		mTVPhoneNumberLabel.setTypeface(font);
		mNext.setTypeface(font);
		
		/*
		 * Spinner Initialization
		 */
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
				this, R.array.country_codes_array, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		mCountryCodeSpinner.setAdapter(adapter);
		mCountryCodeSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int pos, long id) {	
				String[] code = getResources().getStringArray(R.array.country_code_values_array);
				mCountryCode.setText(code[pos]);
				
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
		
		/*
		 * Button OnClickListener Initialization
		 */
		mNext.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				String userProvidedNumber = mPhoneNumber.getText().toString();
				if (userProvidedNumber == null ||
						userProvidedNumber.equals("")) {
					Toast.makeText(ServiceInfo.this, "Please enter your phone number.", Toast.LENGTH_LONG).show();
					return;
				}
				
				
				if (!userProvidedNumber.equals(idNumber)) {
					Toast.makeText(ServiceInfo.this, "[" + idNumber + "] Provided phone number is different yours." +
							"We do not provide alternative mean to confirm your ownership of " + userProvidedNumber +
							". Please enter your phone number.", Toast.LENGTH_LONG).show();
					return;
				}
				
				mHandler.handleMessage(mHandler.obtainMessage());
			}
			
		});
		
		/*
		 * 
		 */
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		
	}

}
